-- global functions


-- TODO: performance issue: test local


--comment to states
--npc's general states :
-- 
--idle
--hunt
--dead (-> in run-part: checks time-to-life til object will be removed)
--
--the run-part of a state is for:
--movement,
--checking/triggering (is there a enemy?)

-- States
state_test = {
name = 'state_test',
starting = {function(t) print('test start function')end, function(t) t.currentstate.ttl = t.currentstate.ttl - 1 print(t.currentstate.ttl) end},
running = {function(t) print('test run function')end, function() print(z) end, zfunc},
ending = {function(t) print('test end function')end},
process = 'starting',
laststate = nil,
nextstate = nil,
ttl = 0, --TODO: needed?
temp = {} -- TODO: evt... weak table?
}

-- handels the movement of the every gameelement, independent of state
-- calculates the new X|Y coordinates


-- clones only the first 'layer' of a table
function clone(t)            -- return a copy of the table t
  local new = {}             -- create a new table
  local i, v = next(t, nil)  -- i is an index of t, v = t[i]
  while i do
    new[i] = v
    i, v = next(t, i)        -- get next index
  end
  return new
end

-- clones a table completly (subtable inclusive)
function deepclone(t)
  local new = {}             -- create a new table
  local i, v = next(t, nil)  -- i is an index of t, v = t[i]
  while i do
    if type(v) == 'table' then -- if value is table then clone it
		v = deepclone(v)
    end
    new[i] = v
    i, v = next(t, i)        -- get next index
  end
  return new
end

function compare(object1x, object1y, object1wid, object1hig, object2x, object2y, object2wid, object2hig)

			if 	object1x + object1wid > object2x and 
				object1x < object2x + object2wid and 
				object1y + object1hig > object2y and 
				object1y < object2y + object2hig
			then return true
			end
end

--[[- countdown

countdown = {}
countdown.status = {}
countdown.limit = {}

function countdown.Start(name,duration)
	countdown.status.[name] = 1
	countdown.limit.[name] = clock.time + duration 


end

--]]

--[[tmr = Timer.new()
tmr:start()
clock ={}

function clock.run()
clock.time = tmr:time()
	clock.hour = math.floor(clock.time/3600000)
	clock.rest = clock.time % 3600000
	clock.minut = math.floor(clock.time/60000)
	clock.rest = clock.rest % 60000
	clock.second = math.floor(clock.rest/1000)
	clock.rest = clock.rest % 1000
	clock.milli = clock.rest
end

function sinus(steps)
steps = steps or 10
return coroutine.wrap(function () i = 0; while true do i = i + 2*math.pi/steps; if i == 2*math.pi then i = 0; end coroutine.yield(math.sin(i)); end  end)
end
--]]
